package com.tianji.aigc.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.listener.Listener;
import jakarta.annotation.PostConstruct;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicReference;



@Slf4j
@Getter
@Configuration
@RequiredArgsConstructor
public class SystemPromptConfig {



    private final NacosConfigManager nacosConfigManager;  //nacos 配置管理类
    private final AIProperties aiProperties;   // 系统提示词 配置属性 在相同包下

    // 使用原子引用，保证线程安全
    private final AtomicReference<String> chatSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> routeAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> generateTitleSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> recommendAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> buyAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> consultAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> knowledgeAgentSystemMessage = new AtomicReference<>();
    private final AtomicReference<String> textSystemMessage = new AtomicReference<>();

    @PostConstruct // 初始化时加载配置  在构造方法执行后执行
    public void init() {
        // 读取配置    tj.ai.prompt.system.chat
        loadConfig(aiProperties.getSystem().getChat(),chatSystemMessage);
        //路由智能体 提示词
        loadConfig(aiProperties.getSystem().getRouteAgent(), routeAgentSystemMessage);
        //AI 根据内容生成标题的系统提示词
        loadConfig(aiProperties.getSystem().getGenerateTitle(), generateTitleSystemMessage);
        //推荐智能体提示词
        loadConfig(aiProperties.getSystem().getRecommendAgent(), recommendAgentSystemMessage);
        //购买智能体提示词
        loadConfig(aiProperties.getSystem().getBuyAgent(), buyAgentSystemMessage);
        //课程咨询智能体提示词
        loadConfig(aiProperties.getSystem().getConsultAgent(), consultAgentSystemMessage);
        //知识讲解智能体提示词
        loadConfig(aiProperties.getSystem().getKnowledgeAgent(), knowledgeAgentSystemMessage);
        //文本内容响应
        loadConfig(aiProperties.getSystem().getText(), textSystemMessage);
    }


    /**
     * 加载配置文件
     * @param chatConfig  配置
     * @param target   目标引用
     */
    private void loadConfig(AIProperties.System.Chat chatConfig, AtomicReference<String> target) {
        try{
            var dataId = chatConfig.getDataId();
            var group = chatConfig.getGroup();
            var timeoutMs = chatConfig.getTimeoutMs();

            //得到配置的文本信息
            String config = nacosConfigManager.getConfigService().getConfig(dataId, group, timeoutMs);
            target.set(config); //设置读取到的配置到目标引用

            log.info("读取{}成功，内容为：{}",target,config);

            //配置监听器，当配置文件有了更新时，同步更新设置
            nacosConfigManager.getConfigService().addListener(dataId, group, new Listener() {

                @Override
                public Executor getExecutor() {
                    return null;
                }

                @Override
                public void receiveConfigInfo(String info) {
                    target.set(info);
                    log.info("更新{}成功，内容为：{}", target, info);

                }
            });

        }catch (Exception e){
            log.error("加载配置失败！");
        }

    }


}
